package com.wwwzh.himalayafm.fragment.disfragment.ranking;


import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.wwwzh.himalayafm.R;
import com.wwwzh.himalayafm.activity.XmlyActivity;
import com.wwwzh.himalayafm.entity.ranking.DisRankHeader;

/**
 * A simple {@link Fragment} subclass.
 */
public class DisRankWebFragment extends Fragment {

    private DisRankHeader.DisRankHeaderContent headerContent;
    private LinearLayout linearLayout;
    private RelativeLayout relativeLayout;
    private Animation animation;

    public static DisRankWebFragment newInstance(DisRankHeader.DisRankHeaderContent headContent) {

        Bundle args = new Bundle();
        args.putSerializable("headContent",headContent);
        DisRankWebFragment fragment = new DisRankWebFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      headerContent = (DisRankHeader.DisRankHeaderContent) getArguments().getSerializable("headContent");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_dis_rank_web, container, false);
        Toolbar toolBar = (Toolbar) rootView.findViewById(R.id.DisRankWebTitle_Tb);
        XmlyActivity activity = (XmlyActivity) getActivity();


        activity.setSupportActionBar(toolBar);
        ActionBar actionBar = activity.getSupportActionBar();
        actionBar.setTitle(headerContent.getShortTitle());
        actionBar.setDisplayHomeAsUpEnabled(true);

        final DrawerLayout drawerLayout = activity.getDrawerLayout();

        final WebView webview = (WebView) rootView.findViewById(R.id.DisRankWeb_WV);


        toolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (headerContent.getUrl().equals(webview.getUrl())){
                    drawerLayout.closeDrawer(Gravity.RIGHT);
                }else {
                    webview.goBack();
                }

            }
        });
//        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(activity,drawerLayout,toolBar,0,0);
//
//        drawerLayout.addDrawerListener(toggle);
//        toggle.syncState();

        linearLayout = (LinearLayout) rootView.findViewById(R.id.DisRankWeb_LL);

        //webview.setWebChromeClient(new WebChromeClient());
        //webview.


        webview.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

            }


            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                removeLoadAnim();
                webview.setVisibility(View.VISIBLE);
            }
        });
        WebSettings s = webview.getSettings();
        s.setJavaScriptEnabled(true);
        webview.loadUrl(headerContent.getUrl());
        webview.setVisibility(View.GONE);
        addLoadAnim();
        return rootView;
    }


    //添加 "加载中" 动画
    private void addLoadAnim() {

        View viewLoad = LayoutInflater.from(getContext()).inflate(R.layout.layout_loading, linearLayout, false);
        relativeLayout = (RelativeLayout) viewLoad.findViewById(R.id.LoadingAnim_RL);
        ImageView imageView = (ImageView) viewLoad.findViewById(R.id.LoadingAnim_IV);
        animation = AnimationUtils.loadAnimation(getContext(), R.anim.anim_load);
        LinearInterpolator lin = new LinearInterpolator();
        animation.setInterpolator(lin);
        imageView.startAnimation(animation);
        linearLayout.addView(relativeLayout);
    }

    //移除 "加载中" 动画
    private void removeLoadAnim() {
        relativeLayout.setVisibility(View.GONE);
        animation.cancel();
        linearLayout.removeView(relativeLayout);
    }


}
